package com.kupurui.asteacher.ui.job;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.ChapterAdapter;
import com.kupurui.asteacher.adapter.SpecialAdapter;
import com.kupurui.asteacher.adapter.ScreenRuleAdapter;
import com.kupurui.asteacher.bean.JobArrangementBean;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.ui.index.ClassDetailsAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.util.PtrInitHelper;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 布置作业
 * Created by Administrator on 2017/7/24.
 */

public class JobArrangementAty extends BaseAty implements LoadMoreHandler, PtrHandler {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.radiobtn_chapter)
    RadioButton radiobtnChapter;
    @Bind(R.id.radiobtn_knowledge)
    RadioButton radiobtnKnowledge;
    @Bind(R.id.radiobtn_special)
    RadioButton radiobtnSpecial;
    @Bind(R.id.radiobtn_questions)
    RadioButton radiobtnQuestions;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @Bind(R.id.fbtn_preview)
    FButton fbtnPreview;
    @Bind(R.id.et_search)
    EditText etSearch;

    PopupWindow popupWindow1;
    PopupWindow popupWindow2;

    JobArrangementBean  jobArrangementBean;
    List<JobArrangementBean.ChapterBean> chapterList;
    ChapterAdapter chapterAdapter;

    List<JobArrangementBean.SpecialBean> specialList;
    SpecialAdapter specialAdapter;

    private String grade_id = "";

    private int is_type = 1;

    @Override
    public int getLayoutId() {
        return R.layout.job_arrangement_aty;
    }

    @Override
    public void initData() {
//        PtrInitHelper.initPtr(this, ptrFrame);
//        ptrFrame.setPtrHandler(this);
//        loadMoreListViewContainer.useDefaultFooter();
//        loadMoreListViewContainer.setAutoLoadMore(true);
//        loadMoreListViewContainer.setLoadMoreHandler(this);
//        loadMoreListViewContainer.loadMoreFinish(false, true);

        chapterList = new ArrayList<>();
        specialList = new ArrayList<>();

        chapterAdapter = new ChapterAdapter(this,chapterList,R.layout.textview_layout);
        specialAdapter = new SpecialAdapter(this,specialList,R.layout.textview_layout);
        listview.setAdapter(chapterAdapter);


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (is_type == 1){
                    Bundle bundle = new Bundle();
                    bundle.putString("id",chapterList.get(position).getChapter_id());
                    bundle.putString("name",chapterList.get(position).getChapter());
                    startActivity(SelKnowledgeAty.class,bundle);
                }else {
                    Bundle bundle = new Bundle();
                    bundle.putString("type","2");
                    bundle.putString("id",specialList.get(position).getSpecial_id());
                    startActivity(JobSelAty.class,bundle);
                }
            }
        });

        fbtnPreview.setText("已选"+ UserManger.subject_id.size()+"道题，点击预览");
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    Bundle bundle = new Bundle();
                    bundle.putString("type","3");
                    bundle.putString("id",etSearch.getText().toString());
                    startActivity(JobSelAty.class,bundle);
                }
                return false;
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().homework(grade_id,this,0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showLoadingContent();
        new Teacher().homework(grade_id,this,0);
        fbtnPreview.setText("已选"+ UserManger.subject_id.size()+"道题，点击预览");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == 100){
                tvRight.setText(data.getStringExtra("grade")+data.getStringExtra("subject"));
                grade_id = data.getStringExtra("grade_id");
            }
        }
    }

    @OnClick({R.id.iv_back,R.id.tv_right,R.id.radiobtn_chapter,R.id.radiobtn_knowledge,R.id.fbtn_preview,R.id.radiobtn_questions,R.id.radiobtn_special})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                startActivityForResult(SelGradeSubjectAty.class,null,100);
                break;
            case R.id.radiobtn_chapter://章节
                listview.setVisibility(View.VISIBLE);
                is_type = 1;
                listview.setAdapter(chapterAdapter);
//                screen1();
                break;
            case R.id.radiobtn_knowledge://知识点
//                screen2();
                break;
            case R.id.fbtn_preview://预览
                if (Toolkit.listIsEmpty(UserManger.subject_id)){
                    showToast("请先选择题目");
                    return;
                }
                startActivity(PreviewAty.class,null);
                break;
            case R.id.radiobtn_questions://我的题库
                startActivity(MyQuestionBankAty.class,null);
                listview.setVisibility(View.GONE);
                break;
            case R.id.radiobtn_special:
                listview.setVisibility(View.VISIBLE);
                is_type = 2;
                listview.setAdapter(specialAdapter);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                jobArrangementBean = AppJsonUtil.getObject(result,JobArrangementBean.class);
                chapterList.clear();
                specialList.clear();
                chapterList.addAll(jobArrangementBean.getChapter());
                specialList.addAll(jobArrangementBean.getSpecial());
                chapterAdapter.notifyDataSetChanged();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void screen1() {
        if (popupWindow1 == null) {
            popupWindow1 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow1.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow1.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("数与式");
            list.add("方程与不等式");
            list.add("函数");
            list.add("图形与几何");
            list.add("图形的变化");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow1.setBackgroundDrawable(new ColorDrawable());
            popupWindow1.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    Bundle bundle = new Bundle();
                    bundle.putString("name",list.get(i));
                    startActivity(SelKnowledgeAty.class,bundle);
                    popupWindow1.dismiss();
                }
            });

            popupWindow1.showAsDropDown(radioGroup);
        } else {
            popupWindow1.showAsDropDown(radioGroup);

        }

    }

    private void screen2() {
        if (popupWindow2 == null) {
            popupWindow2 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow2.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow2.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            list.add("第一章 第一节");
            list.add("第一章 第二节");
            list.add("第二章 第一节");
            list.add("第二章 第二节");
            list.add("第三章 第一节");
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow2.setBackgroundDrawable(new ColorDrawable());
            popupWindow2.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    popupWindow2.dismiss();
                }
            });

            popupWindow2.showAsDropDown(radioGroup);
            popupWindow2.showAsDropDown(radioGroup);

        } else {
            popupWindow2.showAsDropDown(radioGroup);

        }

    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrame, listview, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {

    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {

    }
}
